using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class MidiManager
	{
		#region CONSTRUCTORS
		static MidiManager()
		{
			_data = new MidiData(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static MidiData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
		public static DataTable Insert( string midiName,  string midiName1,  string midiName2,  int categoryID,  string midiURL,  byte[] midiData,  int midiLength,  string midiMono,  string author,  string singer,  string mime,  string location,  long sMSCount,  string createdBy,  DateTime createdDate,  byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[16];
				values[0] = Util.ReplaceNullTo(midiName, DBNull.Value);
				values[1] = Util.ReplaceNullTo(midiName1, DBNull.Value);
				values[2] = Util.ReplaceNullTo(midiName2, DBNull.Value);
				values[3] = categoryID;
				values[4] = Util.ReplaceNullTo(midiURL, DBNull.Value);
				values[5] = Util.ReplaceNullTo(midiData, DBNull.Value);
				values[6] = Util.ReplaceNullTo(midiLength, DBNull.Value);
				values[7] = Util.ReplaceNullTo(midiMono, DBNull.Value);
				values[8] = Util.ReplaceNullTo(author, DBNull.Value);
				values[9] = Util.ReplaceNullTo(singer, DBNull.Value);
				values[10] = Util.ReplaceNullTo(mime, DBNull.Value);
				values[11] = Util.ReplaceNullTo(location, DBNull.Value);
				values[12] = Util.ReplaceNullTo(sMSCount, DBNull.Value);
				values[13] = Util.ReplaceNullTo(createdBy, DBNull.Value);
				values[14] = Util.ReplaceNullTo(createdDate, DBNull.Value);
				values[15] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable Insert(MidiInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[16];
				values[0] = Util.ReplaceNullTo(info.MidiName, DBNull.Value);
				values[1] = Util.ReplaceNullTo(info.MidiName1, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.MidiName2, DBNull.Value);
				values[3] = info.CategoryID;
				values[4] = Util.ReplaceNullTo(info.MidiURL, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.MidiData, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.MidiLength, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.MidiMono, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.Author, DBNull.Value);
				values[9] = Util.ReplaceNullTo(info.Singer, DBNull.Value);
				values[10] = Util.ReplaceNullTo(info.Mime, DBNull.Value);
				values[11] = Util.ReplaceNullTo(info.Location, DBNull.Value);
				values[12] = Util.ReplaceNullTo(info.SMSCount, DBNull.Value);
				values[13] = Util.ReplaceNullTo(info.CreatedBy, DBNull.Value);
				values[14] = Util.ReplaceNullTo(info.CreatedDate, DBNull.Value);
				values[15] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static bool Update( long midiID,  string midiName,  string midiName1,  string midiName2,  int categoryID,  string midiURL,  byte[] midiData,  int midiLength,  string midiMono,  string author,  string singer,  string mime,  string location, byte status)
		{
			_error = "";
			try
			{
				object[] values = new object[14];
				values[0] = midiID;
				values[1] = Util.ReplaceNullTo(midiName, DBNull.Value);
				values[2] = Util.ReplaceNullTo(midiName1, DBNull.Value);
				values[3] = Util.ReplaceNullTo(midiName2, DBNull.Value);
				values[4] = categoryID;
				values[5] = Util.ReplaceNullTo(midiURL, DBNull.Value);
				values[6] = Util.ReplaceNullTo(midiData, DBNull.Value);
				values[7] = Util.ReplaceNullTo(midiLength, DBNull.Value);
				values[8] = Util.ReplaceNullTo(midiMono, DBNull.Value);
				values[9] = Util.ReplaceNullTo(author, DBNull.Value);
				values[10] = Util.ReplaceNullTo(singer, DBNull.Value);
				values[11] = Util.ReplaceNullTo(mime, DBNull.Value);
				values[12] = Util.ReplaceNullTo(location, DBNull.Value);
				values[13] = Util.ReplaceNullTo(status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( MidiInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[14];
				values[0] = info.MidiID;
				values[1] = Util.ReplaceNullTo(info.MidiName, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.MidiName1, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.MidiName2, DBNull.Value);
				values[4] = info.CategoryID;
				values[5] = Util.ReplaceNullTo(info.MidiURL, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.MidiData, DBNull.Value);
				values[7] = Util.ReplaceNullTo(info.MidiLength, DBNull.Value);
				values[8] = Util.ReplaceNullTo(info.MidiMono, DBNull.Value);
				values[9] = Util.ReplaceNullTo(info.Author, DBNull.Value);
				values[10] = Util.ReplaceNullTo(info.Singer, DBNull.Value);
				values[11] = Util.ReplaceNullTo(info.Mime, DBNull.Value);
				values[12] = Util.ReplaceNullTo(info.Location, DBNull.Value);
				values[13] = Util.ReplaceNullTo(info.Status, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( long midiID)
		{
			_error = "";
			try
			{
				object[] values = { midiID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( MidiInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.MidiID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}

        public static DataTable SelectInfo(long mp3ID)
        {
            _error = "";
            try
            {
                object[] values = { mp3ID };
                return _data.Select(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }
		
		public static MidiInfo Select( long midiID)
		{
			_error = "";
			try
			{
				object[] values = { midiID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				MidiInfo info = new MidiInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return MidiManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return MidiManager.Search( "", "", pageIndex, pageSize);
		}

        public static DataTable SearchBy(int intCateId, string strName, DateTime fromDate, DateTime toDate)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = intCateId;
                values[1] = strName;
                values[2] = fromDate;
                values[3] = toDate;

                return _data.SearchBy(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool Approve(int midiId, byte approveValue)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = midiId;
                values[1] = approveValue;
                return _data.Approve(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }
		
		#endregion
		
	}
}